#!/usr/bin/env bash

PASSWD=admin
python=$1
pip=$2
prefix=$3

[[ -z $python ]] && python=python
[[ -z $pip ]] && pip=pip
[[ -z $prefix ]] && prefix=/usr

# wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gzs
\cp plugins/ta-lib-0.4.0-src.tar.gz /tmp
pushd /tmp
tar -xf ta-lib-0.4.0-src.tar.gz
cd ta-lib
./configure --prefix=$prefix
make -j
echo $PASSWD | sudo -S sudo make install
popd

mkdir -p ~/.pip
echo "[global]" > ~/.pip/pip.conf
echo "index-url=http://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf
echo "[install]" >> ~/.pip/pip.conf
echo "trusted-host=mirrors.aliyun.com" >> ~/.pip/pip.conf

echo $PASSWD | sudo -S sudo apt-get remove --purge python-apt
echo $PASSWD | sudo -S sudo apt-get install -y python3.7-dev
echo $PASSWD | sudo -S sudo apt-get install -y python3.7
echo $PASSWD | sudo -S sudo apt-get install -y python3-pip
echo $PASSWD | sudo -S sudo rm -rf /usr/bin/python
echo $PASSWD | sudo -S sudo ln -s /usr/bin/python3.7 /usr/bin/python
echo $PASSWD | sudo -S sudo apt-get install -y python3-tk 
echo $PASSWD | sudo -S sudo apt-get install -f python-apt
echo $PASSWD | sudo -S sudo \cp /usr/lib/python3/dist-packages/apt_pkg.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so

# wget https://bootstrap.pypa.io/get-pip.py
echo $PASSWD | sudo -S sudo python plugins/get-pip.py

# 不能通过源码安装psycopg2，因为官方的插件有巨大的bug
# $pip install --pre --extra-index-url https://rquser:ricequant99@py.ricequant.com/simple/ rqdatac
# $pip install https://vnpy-pip.oss-cn-shanghai.aliyuncs.com/colletion/ibapi-9.75.1-py3-none-any.whl
echo $PASSWD | sudo -S sudo pip install plugins/rqdatac-2.2.0.tar.gz
echo $PASSWD | sudo -S sudo pip install plugins/ibapi-9.75.1-py3-none-any.whl

# Install Python Modules
$pip install -r requirements.txt

# Install local Chinese language environment
echo $PASSWD | sudo -S sudo locale-gen zh_CN.GB18030

# Install vn.py
echo $PASSWD | sudo -S sudo pip install .